package com.inkrl.cart.controller;

import com.inkrl.cart.service.CartService;
import com.jt.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/cart")
    public SysResult queryCart(HttpServletRequest request){
        String token = request.getHeader("token");
        List<Map<String,Object>> cart = cartService.queryCart(token);
        return SysResult.build(200,"查看购物车",cart);
    }

    @PostMapping("/cart")
    public SysResult addCart(HttpServletRequest request,String isbn,Integer count){
        String token = request.getHeader("token");
        int resCode = cartService.addCart(token,isbn,count);
        return SysResult.build(200,"增加购物车",resCode);
    }

    @PutMapping("/cart")
    public void cartNum(String itemId,String num){
        cartService.cartNum(itemId,num);
    }

    @DeleteMapping("/cart")
    public SysResult deleteCart(String rids){
        int resCode = cartService.deleteCart(rids);
        return SysResult.build(200,"删除购物车",resCode);
    }
}
